<template>
  <div class="login_container">
    <div class="login_box">
      <div class="login_img">
        <img src="../assets/logo.png" alt="" />
      </div>
      <div class="login_info">
        <el-form
          ref="loginform"
          label-width="80px"
          :model="loginForm"
          :rules="rules"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              type="password"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
        </el-form>
        <div class="login_button">
          <el-button type="primary" size="mini" @click="login">登录</el-button>
          <el-button type="info" size="mini" @click="cleanInfo"
            >重置密码?</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '请输入3~6字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '请输入6-16位', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    cleanInfo() {
      this.$refs.loginform.resetFields()
    },
    login() {
      this.$refs.loginform.validate(async (valid) => {
        if (!valid) {
          return
        }
        let res = await this.$http.post('login', this.loginForm)
        if (res.data.meta.status !== 200) {
          return this.$message.error('登录失败')
        }
        this.$message.success('登录成功')
        console.log(res.data)
        window.sessionStorage.setItem('token', res.data.data.token)
        this.$router.push('/home')
      })
    },
  },
}
</script>
<style lang="less" scoped>
.login_container {
  background-color: rgb(235, 155, 231);
  height: 100%;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .login_info {
      width: 80%;
      position: absolute;
      top: 40%;
      left: 20px;
    }
    .login_button {
      text-align: right;
    }
  }
  .login_img {
    position: absolute;
    left: 50%;
    top: -40px;
    transform: translateX(-50%);
    width: 100px;
    height: 100px;
    border: 1px dashed #ccc;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px rgb(48, 45, 45);
    background-color: #ccc;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #ccc;
    }
  }
}
</style>